package com.chat.api.web.restctrl;

import cn.hutool.http.HttpUtil;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.metadata.Sheet;
import com.chat.api.test.excel.ExcelListener;
import com.chat.api.web.vo.DemoEntity;
import com.chat.api.web.vo.MessageEntity;
import com.itextpdf.text.io.StreamUtil;
import io.swagger.annotations.ApiOperation;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.alibaba.excel.EasyExcelFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/test")
@Controller
public class TestCtrl {

    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private IdentityService identityService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private RepositoryService repositoryService;

    @GetMapping("/deploy")
    public void deploy() {
        Deployment deployment = processEngine.getRepositoryService()
                .createDeployment()
                .name("approval_test_1")
                .addClasspathResource("processes/approve.bpmn")
                .addClasspathResource("processes/approve.png")
                .deploy();
        System.out.println("部署ID：" + deployment.getId());//1
        System.out.println("部署时间：" + deployment.getDeploymentTime());
    }

    @RequestMapping(value = "/excel", method = RequestMethod.POST)
    @ApiOperation("")
    public void excel(@RequestPart("excel") MultipartFile excel,
                      HttpServletRequest req, HttpServletResponse response) {
        InputStream inputStream = null;
        List<Object> list = null;
        ExcelListener listener = new ExcelListener();
        try {
//            URL conn = new URL("https://ifiletest.2dfire.com/mis/temp/settlement/csv/settlement_cost/rentingexpenseitem1554975619468.xlsx");
//            URLConnection connection = conn.openConnection();
//            inputStream = connection.getInputStream();
            inputStream = excel.getInputStream();
//            inputStream = new BufferedInputStream(inputStream);
//            ExcelReader reader = EasyExcelFactory.getReader(inputStream, listener);
//            List<Sheet> sheets = reader.getSheets();
//            list = EasyExcelFactory.read(inputStream, new Sheet(0));
//            list.subList(0,10);
//            new AnalysisEventListener<Object>() {
//                @Override
//                public void invoke(Object object, AnalysisContext context) {
//
//                }
//
//                @Override
//                public void doAfterAllAnalysed(AnalysisContext context) {
//                    Sheet currentSheet = context.getCurrentSheet();
//                    System.out.println(currentSheet);
//                }
//            };

            ExcelListener excelListener = new ExcelListener();
            inputStream = new BufferedInputStream(inputStream);
            ExcelReader reader = EasyExcelFactory.getReader(inputStream, listener);
            List<Sheet> sheets = reader.getSheets();
//            for (Object o : list) {
//                System.out.println(o);
//            }
            List<Object> datas = null;
            for (Sheet sheet : sheets) {
                reader.read(sheet);
                List<Object> read = EasyExcelFactory.read(excel.getInputStream(), new Sheet(1));
                for (Object data2 : read) {
                    System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
                    System.out.println(data2);
                    System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
                }
            }
//            if (datas.get(1).size() > 10) {
//                datas.subList(0, 10);
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("1");
        list = new ArrayList<>();
        System.out.println(list);
    }

    private static ArrayList<Object> getList() {
        ArrayList<Object> objects = new ArrayList<>();
        objects.add("1");
        return objects;
    }


}
